/*
 * @Author: Tendo
 * @Date: 2022-04-28 13:42:09
 * @LastEditTime: 2022-04-28 15:06:20
 * @LastEditors: Tendo
 * @Description: 
 * @FilePath: \TS\映射类型.ts
 */

// OptionsFlags 会遍历 Type 所有的属性，然后设置为布尔类型
type OptionsFlags<Type> = {
    [Property in keyof Type]: boolean;
};


type FeatureFlags = {
    darkMode: () => void;
    newUserProfile: () => void;
};

type FeatureOptions = OptionsFlags<FeatureFlags>;


// 删除属性中的只读属性
type CreateMutable<Type> = {
    -readonly [Property in keyof Type]: Type[Property];
};

interface LockedPerson {
    readonly name: string
    age: number
    readonly job?: string
}

type UnlockedPerson = CreateMutable<LockedPerson>;


// 删除属性中的可选属性
type Concrete<Type> = {
    [Property in keyof Type]-?: Type[Property];
};

type UnlockedPerson2 = Concrete<LockedPerson>;


// 在映射类型中使用 as 语句实现键名重新映射
type MappedTypeWithNewProperties<Type> = {
    [Properties in keyof Type as "NewKeyType"]: Type[Properties]
}
interface Person2 {
    name: string;
    age: number;
}

type NewPerson = MappedTypeWithNewProperties<Person>


// 也可以利用条件类型返回一个 never 从而过滤掉某些属性:
type RemoveKindField<Type> = {
    [Property in keyof Type as Exclude<Property, "name">]: Type[Property]
};
type NewPerson2 = RemoveKindField<Person2>


